<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="主力合约" prop="localSymbol">
          <el-input v-model="filterForm.localSymbol" clearable placeholder="主力合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="到期时间" prop="expireDate">
          <el-date-picker 
          clearable
            v-model="filterForm.expireDate"
            value-format="yyyy-MM-dd"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="按风险" >
            <el-radio-group v-model="filterForm.risk"  @input="handleFilter">
            <el-radio-button label="0">全部</el-radio-button>
            <el-radio-button label="1">低风险</el-radio-button>
            <el-radio-button label="2">中风险</el-radio-button>
            <el-radio-button label="3">高风险</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="类型" >
            <el-radio-group v-model="filterForm.style"  @input="handleFilter">
            <el-radio-button label="0">全部</el-radio-button>
            <el-radio-button label="1">看涨</el-radio-button>
            <el-radio-button label="2">看跌</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="交易" >
            <el-radio-group v-model="filterForm.allowed"  @input="handleFilter">
            <el-radio-button label="-1">全部</el-radio-button>
            <el-radio-button label="0">未开启</el-radio-button>
            <el-radio-button label="1">已开启</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="关键字" prop="symbol">
          <el-input v-model="filterForm.symbol" clearable placeholder="合约关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
      </el-form>
      <el-button  @click="getList()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column
        prop="symbol"
        label="合约">
        <template slot-scope="{row}">
          <div @click="handleDetailButton(row)">{{ row.symbol }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="localSymbol"
        label="主力合约" width="100">
        <template slot-scope="{row}">
          <div>{{ row.localSymbol }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="执行价"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.exercise_price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类型"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.style == 1" type="danger">看多</el-tag>
          <el-tag v-else type="success">看跌</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="交易状态"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.allowed == 1" @click="onOptionChangeAllowedButton(row.ID,0)">开启</el-tag>
          <el-tag v-else @click="onOptionChangeAllowedButton(row.ID,1)">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="风险等级"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.riskWeight == 1" type="success">低风险</el-tag>
          <el-tag v-else-if="row.riskWeight == 2" type="warning" >中风险</el-tag>
          <el-tag v-else-if="row.riskWeight == 3"  type="danger">高风险</el-tag>
          <el-tag v-else >未定义</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="上市日"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.market_date }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="到期日"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.expire_date }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="持仓"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.position }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="发布时间"
        width="200">
      </el-table-column>
      <el-table-column
        label="操作"
        width="300">
        <template slot-scope="{row}">
            <el-button type="danger" @click="handleDetailButton(row)" >详情</el-button>
            <el-button type="info" @click="handleQuotationButton(row)" >行情</el-button>
            <el-button type="waring" @click="handlePolicyButton(row)" >策略</el-button>
        </template>
      </el-table-column>
      </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />

  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

import {getOptionTradeList,changeOptionAllowed} from '@/api/knowledge/option'

export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      connected:"loading",
      openSubscribeHidden:false,
      industryOptions:[],
      subIndustryOptions:[],
      allowedRefreshSubIndustry:false,
      feelingsKeywordList:[{name:''}],
      projectForm:{
      },
      filterForm: {
        page: 1,
        size: 10,
        allowed:0,
        risk:0,
        localSymbol:"",
        style: 0,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
  },
  
  methods: {
    getList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getOptionTradeList(_this.filterForm).then((response) => {
        console.log(response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleDetailButton:function(item){
      this.$router.push({ path:'/knowledge/option-detail', query: {"optionID":item.ID} })

    },
    handleQuotationButton:function(item){
      this.$router.push({ path:'/quantization/market', query: {"symbol":item.symbol} })

    },
    handlePolicyButton:function(item){
      this.$router.push({ path:'/quantization/policy', query: {"symbol":item.symbol} })
    },
    onOptionChangeAllowedButton:function(optionID,allowed){
      let _this = this;
      let _formData = {
        "optionID":optionID,
        "allowed":allowed
      }
      changeOptionAllowed(_formData).then((response) => {
        console.log(response)
          if (response.code === 200) {
            _this.getList()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleShow:function(item){
      this.$router.push({ path: 'capital-detail', query: { 'capitalID': item.ID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    }
  }
}
</script>
<style>

</style>